function Movement_Code() {
	var buffer = new StringBuffer();
	buffer.append('<div id="find" class="finds"><div class="searchinput ui-border-radius"><input id="keyword" class="bordersolid" type="text" value="" onblur="Show_Search()" onkeyup="Movement_Search();"/></div><div id="searchicon" class="searchicon"><img src="img/jing.jpg"/></div><div id="searchicons" class="searchicons"><img src="img/jings.jpg"/></div><div class="screen"><button id="screen" class="ui-border-radius">筛选</button></div></div>');
	buffer.append('<div class="horizen border-bottom"></div>');
	return buffer.toString();
};

function Init_Movement_Code()
{
	get("find").addEventListener("tap",function(){
		$("#searchicon").hide();
	    $("#searchicons").show();
	    $("#keyword").attr("placeholder","调拨单号"); 
		if($("keyword").val()!="")
		{
			//WorkMate_Search();
		}
	});
	get("screen").addEventListener("tap",function(){
		OpenScreen(Movement_Search_Code(),function(){
			Init_Movement_Search();
		},Movement_Search);
		stopEvent();
	});
};

function Init_Movement_Search(){
	mui(".option").on("tap",".date",function(){
		obj=this;
			var t=new Date();
			if($(this).val()!="")
			{
				var year_month_day=$(this).val().split("-");
				t.setFullYear(year_month_day[0]);
				t.setMonth(year_month_day[1]-1);
				t.setDate(year_month_day[2]);
			}
			plus.nativeUI.pickDate( function(e){
				var d=e.date;
				$(obj).val((d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate()).ToDate());
			},function(e){
				console.log( "未选择日期："+e.message );
			},{date:t});
	});

};

function Movement_Search_Code()
{
	var buffer = new StringBuffer();
	buffer.append('<div class="searchline" style="margin-top:10px;"><div class="names">调拨时间</div><div class="option"><input id="begindate_search" type="text" class="date" placeholder="开始时间" readonly="readonly"/><input id="enddate_search" type="text" class="date" placeholder="结束时间" readonly="readonly"/></div></div>');
	return buffer.toString();
};

function Movement_Data(data) {
	var buffer = new StringBuffer();
	$(data).each(function(i,info){
		buffer.append('<div class="orderlist border-bottom" id="'+info.ID+'"><div class="ordernum">'+info.Num+'</div><div class="orderprice">总额：'+info.AllMoney.toFixed(2)+'</div><div class="orderinfo"><div class="state">操作人：'+ info.RealName + '</div><div class="lastconntime">'+info.CreateDate.ToTime()+'</div></div></div>');
	});
	return buffer.toString();
};

function Receive_Movement(pageindex)
{
	var data = {};
			data.act = "Find_Movement";
			data.userid=userid;
			data.comid=comid;
			data.pageindex=pageindex;
			data.source=source;
			data.pagesize=PAGE_SIZE20;
			data.keyword = $("#keyword").val();
		    data.timer = RUndefined($("#begindate_search").val()) + "～" + RUndefined($("#enddate_search").val());
		    data.sign=GetSign(data.act);
			mui.getJSON(crm_domain, data, function(json) {
				if(json.data.list.length>0)
				{
					if(pageindex==1)
					{
						$("#contentlist1").html(Movement_Data(json.data.list));
					}else{
						$("#contentlist1").append(Movement_Data(json.data.list));
					}
					setTimeout(function(){
						Init_Movement();
						myScroll1.refresh();
					},500); //需要延迟500毫秒加载，否则当数量少时不能下拉
					if(json.data.list.length!=data.pagesize)
					{
						$("#loading1").html("");
					}else{
						$("#loading1").html('<img src="img/loading.gif"/>');
					}
				}else
				{
					if(pageindex==1)
					{
						$("#contentlist1").html("");
						$("#loading1").html("主人，没有找到您需要的数据");
					}else
					{
						$("#loading1").html("没有更多了");
					}
				}
				
         		pageindex = parseInt($("#divmsg1").attr("pageindex"))+1;
         		$("#divmsg1").attr("pageindex",pageindex);
			});
};

function Movement_Search(){
	pageindex=1;
	$("#divmsg1").attr("pageindex","0");
	Receive_Movement(1);
};

function Init_Movement()
{
	mui("#contentlist1").off("tap",".orderlist");
	mui("#contentlist1").off("longtap",".orderlist");
	mui("#contentlist1").on("tap",".orderlist",function(){
		OpenWindow2(2,"调拨详情","操作",function(){
			MovementTool();
		});
		obj=$(this);
		$("#contentlist2").html(MovementInfo_Code());
		get("contentlist2").addEventListener("tap",function(){
			if($(".customertool").length>0)
			{
				$(".customertool").hide();
			}
		});
		Init_MovementDetail();
	});
	mui("#contentlist1").on("longtap",".orderlist",function(){
		obj=$(this);
		OpenWindow(3,"调拨单","保存",function(){
				Save_Movement();
			});
			$("#contentlist3").html(MovementAdd_Code(obj.attr("id")));
			Init_MovementAdd();
	});
};


function MovementAddTool()
{
	if($("#movementaddtool").length>0)
	{
		$("#movementaddtool").show();
	}else
	{
		$("body").append('<div id="movementaddtool" class="customertool" style="height:120px;"><ul><li id="movementadd">添加采购订单</li><li id="movementdelete">添加采购退单</li></ul></div>');
		//初始化工具条
		
        
		get("movementadd").addEventListener("tap",function(){
        	OpenWindow(2,"调拨单","保存",function(){
				Save_Movement();
			});
			$("#contentlist2").append(MovementAdd_Code(0));
			Init_MovementAdd();
			$("#movementaddtool").hide();
        });
        
        get("movementdelete").addEventListener("tap",function(){
        	OpenWindow(2,"采购退单","保存",function(){
				Save_Movement();
			});
			$("#contentlist2").append(MovementAdd_Code(0,"purchase",2));
			Init_MovementAdd();
			$("#movementaddtool").hide();
        });
	}
};
